import hashlib
import time
import requests
from urllib import parse
import json

class TaoBao():

    def __init__(self,keyword):
        self.keyword = keyword
        self.session = requests.Session()
        self.cookies = {}
        self.data = {}
        self.params = {}
        self.headers = {}
        for page in range(1,100+1):
            if page == 1:
                self.get_url()
                self.get_m_h5_tk()
                self.get_cna()
                self.get_data(page)
            else:
                self.get_data(page)

    def to_cookie(self):
        cookie = ''
        for key, value in self.cookies.items():
            if '_m_h5' in key or 'cna' == key:
                cookie += key + '=' + value + '; '
        return cookie

    def h(self,a):
        md5 = hashlib.md5()
        md5.update(a.encode())
        return md5.hexdigest()

    # 获取uland地址和clk1值
    def get_url(self):
        headers = {
            "Host":"redirect.simba.taobao.com",
            "Upgrade-Insecure-Requests":"1",
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36",
            "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
            "Accept-Encoding":"gzip, deflate",
            "Accept-Language":"zh-CN,zh;q=0.9",
            "Connection":"keep-alive"
        }
        url = 'http://redirect.simba.taobao.com/rd?c=un&w=unionsem&k=74175f13c74157df&p=mm_26632258_3504122_32538762&b=R9JQC0peugGMqgRlL5O&s={keyword}&f=https%3A%2F%2Fuland.taobao.com%2Fsem%2Ftbsearch%3Frefpid%3Dmm_26632258_3504122_32538762%26keyword{keywords}'.format(keyword=parse.quote(self.keyword),keywords=parse.quote(parse.quote('='+self.keyword)))
        response = self.session.get(url,headers=headers,allow_redirects=False)
        self.clk1 = response.headers.get('Location').split('&')[2]
        self.uland_url = response.headers.get('Location')

    # 获取_m_h5_tk和_m_h5_tk_enc
    def get_m_h5_tk(self):
        self.data = {
            "keyword": self.keyword,
            "ppath": "", "loc": "",
            "minPrice": "",
            "maxPrice": "",
            "ismall": "",
            "ship": "",
            "itemAssurance": "",
            "exchange7": "",
            "custAssurance": "",
            "b": "",
            "clk1": self.clk1,
            "pvoff": "",
            "pageSize": "100",
            "page": "",
            "elemtid": "1",
            "refpid": "mm_26632258_3504122_32538762",
            "pid": "430673_1006",
            "featureNames": "spGoldMedal,dsrDescribe,dsrDescribeGap,dsrService,dsrServiceGap,dsrDeliver, dsrDeliverGap",
            "ac": "",
            "wangwangid": "",
            "catId": ""
        }
        self.timestamp = str(time.time()).replace('.', '')[:13]
        self.params = {
            'jsv': '2.4.0',
            'appKey': '12574478',
            't': self.timestamp,
            'sign': self.h('undefined' + "&" + self.timestamp + "&" + '12574478' + "&" + json.dumps(self.data,ensure_ascii=False).replace(' ', '')),
            'api': 'mtop.alimama.union.sem.landing.pc.items',
            'v': '1.0',
            'AntiCreep': 'true',
            'dataType': 'jsonp',
            'type': 'jsonp',
            'ecode': '0',
            'callback': 'mtopjsonp1',
            'data': json.dumps(self.data, ensure_ascii=False).replace(' ', '')
        }
        self.headers = {
            'host': 'h5api.m.taobao.com',
            'accept': '*/*',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-Hans-CN, zh-Hans; q=0.8, en-US; q=0.5, en; q=0.3',
            'referer': self.uland_url,
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
        }
        self.session.get('https://h5api.m.taobao.com' + '/h5/mtop.alimama.union.sem.landing.pc.items/1.0/?' + parse.urlencode(self.params), headers=self.headers)
        for key, value in self.session.cookies.items():
            self.cookies[key] = value
        # self.token = self.cookies['_m_h5_tk'].split('_')[0]
        self.token = self.cookies['_tb_token_'].split('_')[0]

    # 获取cna
    def get_cna(self):
        headers = {
            'authority': 'log.mmstat.com',
            'method': 'GET',
            'path': '/eg.js',
            'scheme': 'https',
            'accept': '*/*',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'referer': self.uland_url,
            'sec-fetch-dest': 'script',
            'sec-fetch-mode': 'no-cors',
            'sec-fetch-site': 'cross-site',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
        }
        self.session.get('https://log.mmstat.com/eg.js', headers=headers)
        for key, value in self.session.cookies.items():
            self.cookies[key] = value

    # 获取数据
    def get_data(self, page):
        self.data['page'] = "" if page == 1 else page
        self.data['ac'] = "" if page == 1 else self.cookies.get('cna')
        self.timestamp = str(time.time()).replace('.', '')[:13]
        self.params['t'] = self.timestamp
        self.params['sign'] = self.h(self.token + "&" + self.timestamp + "&" + '12574478' + "&" + json.dumps(self.data,ensure_ascii=False).replace(' ',''))
        self.params['data'] = json.dumps(self.data, ensure_ascii=False).replace(' ', '')
        self.headers['cookie'] = self.to_cookie()
        response = self.session.get('https://h5api.m.taobao.com' + '/h5/mtop.alimama.union.sem.landing.pc.items/1.0/?' + parse.urlencode(self.params), headers=self.headers)
        json_data = json.loads(response.text.replace('mtopjsonp1(','').replace(')',''))
        print(json_data)

if __name__ == '__main__':
    TaoBao('男装')	